import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { _HttpClient, SettingsService, TitleService } from '@delon/theme';
import { OrderService } from '../../../service/order/order.service';
import { SimpleTableComponent, SimpleTableColumn } from '@delon/abc/src/simple-table';
import { CommService } from '../../../service/comm/comm.service';
import { ServiceResult } from '@core/constant';
import { formatDate } from '@angular/common';
import { Router } from '@angular/router';
import { EmitterService } from 'app/service/emit.service';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { UserSelectModalComponent } from '@shared/component/users/user-select-modal.component';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { SmsService } from '../../../service/sms/sms.service';

@Component({
  selector: 'delivery-cancel',
  templateUrl: './delivery-cancel.component.html',
  styleUrls: ['./delivery-cancel.component.less']
})
export class DeliveryCancelComponent implements OnInit, OnDestroy {
    barcode;
    loading = false;    
    @ViewChild('st') st: SimpleTableComponent;
    data: any[] = [];

    columns: SimpleTableColumn[] = [
        { title: '#', index: 'lno' },
        { title: '品牌', index: 'brand', render: 'brand', i18n: 'brand' },
        { title: '型号', index: 'style', i18n: 'style' },
        { title: '色号', index: 'colorCd', i18n: 'colorCd' },        
        { title: '扫描数量', index: 'qty', type: "number", i18n: 'qty' }     
    ];
    constructor(
      private orderServ: OrderService,
      private emit: EmitterService,
      private titleService: TitleService,
      private router: Router,
      private setting: SettingsService,
      private modalService: NzModalService,
      private msg: NzMessageService,
      private sms: SmsService
    ) {}

    ngOnInit() {        
      // 隐藏reusetab
      setTimeout(() => {
        this.emit.sendMessage("HIDE_REUSE_TAB");
      }, 100);    
        this.titleService.setTitle("发货撤销");    
    }
    
    back() {
      this.router.navigate(['/workplace/list']);
    }
    
    // 显示reusetab
    ngOnDestroy() {    
      this.emit.sendMessage("SHOW_REUSE_TAB");
    }

    cancel(event) {
        if (event.keyCode === 13) {  
            this.deliveryCancel();
        }
    }

    deliveryCancel() {        
        if (this.barcode) {
            this.loading = true;
            this.sms.cancelAgent2Client(this.barcode).pipe(catchError(err => {
                return of(err)
            })).subscribe(ret => {
                this.loading = false;
                if (ret.status === 200) {
                    this.barcode = "";
                    this.msg.success('Successful');
                    let flag = false;
                    let tmpList = [...this.data];
                    for (let i = 0; i < tmpList.length; i++) {
                        if (tmpList[i].productCd === ret.data['PRODUCT_CD'] && tmpList[i].lno === ret.data['TRANLNO']) {
                            tmpList[i].qty ++;
                            flag = true;
                            break;
                        }
                    }                   
                    if (!flag) {                        
                        tmpList.push({ 
                            productCd: ret.data['PRODUCT_CD'],
                            lno: ret.data['TRANLNO'], 
                            brand: ret.data['BRAND'],  
                            style: ret.data['STYLE'],
                            colorCd: ret.data['COLOR_CD'],
                            qty: 1
                        })                        
                    } 
                    this.data = tmpList;
                } else {
                    this.msg.error(ret.message);
                }
            })
        }
    }
}